<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jedrzej\Pimpable\PimpableTrait;

class StoreBargain extends Model
{
    use HasFactory;
    use PimpableTrait;
    use SoftDeletes;

    /**
     *允许批量赋值的字段
     */
    public $fillable = [
        'pic',
        'product_id',
        'rotation_chart',
        'title',
        'price',
        'min_price',
        'introduction',
        'start_at',
        'stop_at',
        'participants',
        'people_num',
        'quota_show',
        'quota',
        'unit_name',
        'temp_id',
        'description',
        'shop_id',
        'sort',
        'status',
    ];

    protected $casts = ['rotation_chart' => 'json'];

    public $searchable = [
        'title',
        'id',
        'active_status',
        'start_at',
        'shop_at',
        'status'
    ];

    /**
     * 设置模糊搜索
     */
    public $searchableModels = [
        'title' => '%field%',
    ];

    /**
     * 设置枚举值
     */
    public $activeStatusEnum = [
        'haveInHand' => '进行中',
        'completed' => '已结束',
        'noCompleted' => '未开始',
    ];

    /**
     *可排序字段
     */
    public $sortable = [
        'created_at',
        'id',
        'min_price',
        'sales',
        'buyers',
        'is_recommend',
        'status',
    ];

    /**
     * 默认倒序
     */
    protected $defaultSortCriteria = ['id,desc'];

    /**
     * 关联商品模型
     */

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'product_id');
    }

    /**
     * 关联用户模型
     */

    public function storeBargainUser()
    {
        return $this->belongsTo(StoreBargainUser::class, 'bargain_user_id', 'id');
    }

    /**
     * 关联店铺模型
     */

    public function systemStore()
    {
        return $this->belongsTo(SystemStore::class, 'shop_id', 'id');
    }

    /**
     * 关联单规格的模型
     */

    public function attrResult()
    {
        return $this->belongsTo(ProductAttrResult::class,'product_id','product_id');
    }

}
